package com.song.studylineserver.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.studylineserver.domain.Role;
import com.song.studylineserver.domain.User;
import com.song.studylineserver.service.PostService;
import com.song.studylineserver.service.RoleService;
import com.song.studylineserver.service.UserService;
import com.song.studylineserver.mapper.UserMapper;
import com.song.studylineserver.util.Loggers;
import com.song.studylineserver.util.Result;
import com.song.studylineserver.util.SpringUtils;
import com.song.studylineserver.util.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Swtao
* @description 针对表【sl_user(用户信息表)】的数据库操作Service实现
* @createDate 2022-01-14 17:28:38
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    UserMapper userMapper;


    /**
     * 根据用户名获取用户
     * @param username
     * @return
     */
    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);

    }


    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 校验用户是否有数据权限
     *
     * @param userId 用户id
     */
    @Override
    public void checkUserDataScope(Long userId)
    {
        if (!User.isAdmin(Convert.toLong(StpUtil.getLoginId())))
        {
            User user = new User();
            user.setUserId(userId);
            List<User> users = SpringUtils.getAopProxy(this).selectUserList(user);
            if (StringUtils.isEmpty(users))
            {
                throw new RuntimeException("没有权限访问用户数据！");
            }
        }
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public User selectUserById(Long userId)
    {
        return userMapper.selectUserById(userId);
    }


}




